package com.easy.controller;

import com.easy.bean.Category;
import com.easy.bean.Product;
import com.easy.service.ICategoryService;
import com.easy.service.IProductService;
import com.easy.util.LayData;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    IProductService productService;

    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text, Integer categoryid){
        List<Product> list;
        int count;

        // 如果有分类ID参数，按分类筛选商品
        if (categoryid != null && categoryid > 0) {
            list = productService.getListByCategory(page, text, categoryid);
            count = productService.getCountByCategory(text, categoryid);
        } else {
            list = productService.getList(page, text);
            count = productService.getCount(text);
        }

        return new LayData(count, list);
    }


    @RequestMapping("delete")
    @ResponseBody
    public int delete(int productid){
        return productService.deleteProduct(productid);
    }

    @RequestMapping("add")
    @ResponseBody
    public int add(Product product){
        return productService.addProduct(product);
    }

    @RequestMapping("edit")
    @ResponseBody
    public int edit(Product product){
        return productService.editProduct(product);
    }

    @RequestMapping("getProductById")
    @ResponseBody
    public Product getProductById(int productid){
        return productService.getProductById(productid);
    }
}
